package com.sduept.nwld.dataserver.controller.daily;

import com.sduept.bigdata.web.jsf.controller.AbstractController;

import javax.annotation.PostConstruct;
import javax.servlet.http.HttpSession;
import java.util.Calendar;
import java.util.Date;

public class DailyReportAbstractController extends AbstractController {

	/**
	 *
	 */
	private static final long serialVersionUID = 3529148636750672863L;
	public Date queryDate1;
	public Date queryDate2;

	@PostConstruct
	protected void initParamter() {
		HttpSession session = getHttpSession();
		queryDate1 = (Date) session.getAttribute("dailyReportQueryStart");
		queryDate2 = (Date) session.getAttribute("dailyReportQueryEnd");
		if (queryDate1 == null || queryDate2 == null) {
			initDailyReportParamter(session);
		}
	}

	public void resetqueryDate() {
		HttpSession session = getHttpSession();
		if (queryDate1 == null || queryDate2 == null) {
			initDailyReportParamter(session);
		}
	}

	public void initDailyReportParamter(HttpSession session) {
		Calendar last = Calendar.getInstance();
		last.set(last.get(Calendar.YEAR), last.get(Calendar.MONDAY), last.get(Calendar.DAY_OF_MONTH), 0, 0, 0);
		last.add(Calendar.DATE, -7);
		queryDate1 = last.getTime();
		Calendar now = Calendar.getInstance();
		now.set(now.get(Calendar.YEAR), now.get(Calendar.MONDAY), now.get(Calendar.DAY_OF_MONTH), 23, 59, 59);
		queryDate2 = now.getTime();
		session.setAttribute("dailyReportQueryStart", queryDate1);
		session.setAttribute("dailyReportQueryEnd", queryDate2);
	}

	public void refreshQueryParamter() {
		Calendar c1 = Calendar.getInstance();
		c1.setTime(queryDate1);
		c1.set(Calendar.HOUR_OF_DAY, 0);
		c1.set(Calendar.MINUTE, 0);
		c1.set(Calendar.SECOND, 0);
		c1.set(Calendar.MILLISECOND, 0);

		Calendar c2 = Calendar.getInstance();
		c2.setTime(queryDate2);
		c2.set(Calendar.HOUR_OF_DAY, 23);
		c2.set(Calendar.MINUTE, 59);
		c2.set(Calendar.SECOND, 59);
		c2.set(Calendar.MILLISECOND, 999);
		HttpSession session = getHttpSession();

		session.setAttribute("dailyReportQueryStart", c1.getTime());
		session.setAttribute("dailyReportQueryEnd", c2.getTime());
	}


	public Date getQueryDate1() {
		return queryDate1;
	}

	public void setQueryDate1(Date queryDate1) {
		this.queryDate1 = queryDate1;
	}

	public Date getQueryDate2() {
		return queryDate2;
	}

	public void setQueryDate2(Date queryDate2) {
		this.queryDate2 = queryDate2;
	}

}
